package colin.spring.boot.sample.web.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import colin.spring.boot.sample.web.dao.UserDao;
import colin.spring.boot.sample.web.pojo.UserEntity;

@Service
@Transactional
public class UserService {

	@Autowired
	private UserDao userDao;

	/**
	 * 
	 * 方法描述：获取所有用户信息 注意事项：
	 * 
	 * @return
	 * @Exception 异常对象
	 */
	public List<UserEntity> getAllUsersInfo() {
		Iterable<UserEntity> userIteraator = userDao.findAll();
		if (userIteraator != null) {
			return this.transerIteraEntities(userIteraator);
		} else {
			return null;
		}
	}

	/**
	 * 
	 * 方法描述：保存用户信息 注意事项：
	 * 
	 * @param username
	 * @param password
	 * @Exception 异常对象
	 */
	public void createUserInfo(String username, String password) {
		UserEntity userEntity = new UserEntity();
		userEntity.setUsername(username);
		userEntity.setPassword(password);
		userDao.save(userEntity);
	}

	public List<UserEntity> findUserInfo(Map<String, Object> params) {
		if (params.containsKey("username") && params.containsKey("email")
				&& params.containsKey("sex")) {
			return userDao.findByUsernameAndEmailAndSex(params.get("username")
					.toString(), params.get("email").toString(),
					params.get("sex").toString());
		} else if (params.containsKey("username")
				&& params.containsKey("email") && !params.containsKey("sex")) {
			return userDao.findByUsernameAndEmail(params.get("username")
					.toString(), params.get("email").toString());
		} else if (params.containsKey("username")
				&& !params.containsKey("email") && !params.containsKey("sex")) {
			return userDao.findByUsername(params.get("username").toString());
		} else if (!params.containsKey("username")
				&& params.containsKey("email") && params.containsKey("sex")) {
			return userDao.findByEmailAndSex(params.get("email").toString(),
					params.get("sex").toString());
		} else if (!params.containsKey("username")
				&& params.containsKey("email") && !params.containsKey("sex")) {
			return userDao.findByEmail(params.get("email").toString());
		} else if (!params.containsKey("username")
				&& !params.containsKey("email") && params.containsKey("sex")) {
			return userDao.findBySex(params.get("sex").toString());
		} else if (params.containsKey("username")
				&& !params.containsKey("email") && params.containsKey("sex")) {
			return userDao.findByUsernameAndSex(params.get("username")
					.toString(), params.get("sex").toString());
		} else {
			return null;
		}
	}

	/**
	 * 
	 * 方法描述：根据真实姓名查询用户名 注意事项：
	 * 
	 * @param truename
	 * @return
	 * @Exception 异常对象
	 */
	public List<String> findUsername(String truename) {
		return userDao.findByTruename(truename);
	}

	/**
	 * 
	 * 方法描述：转换对象 注意事项：
	 * 
	 * @param userIterable
	 * @return
	 * @Exception 异常对象
	 */
	public List<UserEntity> transerIteraEntities(
			Iterable<UserEntity> userIterable) {
		Iterator<UserEntity> userIterator = userIterable.iterator();
		List<UserEntity> userList = new ArrayList<UserEntity>();
		while (userIterator.hasNext()) {
			userList.add(userIterator.next());
		}
		return userList;
	}
}
